package com.yanqu.road.server.gameplayer.module.activity.dunhuangshiku;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.dunhuangshiku.DunHuangShiKuEventConfig;
import com.yanqu.road.entity.activity.dunhuangshiku.DunHuangShiKuServerUserData;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.logic.bussiness.player.activity.UserDunHuangShiKuActivityBusiness;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.dunhuangshiku.DunHuangShiKuMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

/**
 * @Description 敦煌石窟玩家对象
 * @Author cwq
 * @Date 2021/4/15 17:09
 */
public class DunHuangShiKuModule extends GeneralModule {

    /**
     * 玩家信息
     */
    public DunHuangShiKuServerUserData baseData;

    /**
     * 当前所在区域id （日志需要,跟商品id一样 同时被get/set）
     */
    public int regionId;

    /**
     * 队伍id（日志需要,跟商品id一样 同时被get/set）
     */
    public int teamId;

    /**
     * 创建房间预扣的道具
     */
    public Property createTeamProperty;

    /**
     * 创建房间预扣的道具
     */
    public Property nextRegionProperty;

    /**
     * 配置项
     */
    public DunHuangShiKuModule(GamePlayer player) {
        super(player);
    }

    @Override
    public void afterLogin() {
        sendConfigToClient();
    }

    public void initSystem() {
        sendConfigToClient();
    }

    @Override
    public boolean loadData() {
        if (DunHuangShiKuMgr.activityInfo != null) {
            baseData = UserDunHuangShiKuActivityBusiness.getDunHuangShiKuServerUserData(DunHuangShiKuMgr.activityInfo.getActivityId(), player.getUserId());
            initBaseData(DunHuangShiKuMgr.activityInfo.getActivityId());
        }
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        if (baseData != null) {
            if (baseData.isUpdateOption()) {
                UserDunHuangShiKuActivityBusiness.updateDunHuangShiKuServerUserData(baseData);
            } else if (baseData.isInsertOption()) {
                UserDunHuangShiKuActivityBusiness.addDunHuangShiKuServerUserData(baseData);
            }
        }
        return true;
    }

    public void sendConfigToClient() {
        if (DunHuangShiKuMgr.activityInfo != null && SystemOpenMgr.systemOpen(player, eSystemId.DunHuangShiKuActivity.getValue())) {
            initBaseData(DunHuangShiKuMgr.activityInfo.getActivityId());
            player.sendPacket(ClientProtocol.U_DUN_HUANG_SHI_KU_ACTIVITY, DunHuangShiKuMgr.getDunHuangShiKuActivity());
        }
    }

    /**
     * 是否可以到账成功 不判断时间 只要有就可以到账
     *
     * @param mallId
     * @return
     */
    public boolean canChargeSucceed(int mallId) {
        ActivityInfo activityInfo = DunHuangShiKuMgr.activityInfo;
        if (!ActivityMgr.activityInShowTime(activityInfo)) {
            return false;
        }
        if (baseData == null) {
            return false;
        }
        // 判断是否能购买
        return baseData.getTouchMallMap().containsKey(mallId);
    }

    /**
     * 商城购买
     *
     * @param mallId
     */
    public void shopBuy(int mallId) {
        try {
            if (!ActivityMgr.activityInShowTime(DunHuangShiKuMgr.activityInfo)) {
                return;
            }
            if (baseData == null) {
                return;
            }
            baseData.reduceTouchMallTimes(mallId, 1);
            if(baseData.getTouchMallTimes(mallId) <= 0) {
                baseData.removeTouchMall(mallId);
            }
        } catch (Exception ex) {
            log.error(ex);
        } finally {

        }

    }

    /**
     * 是否可下单
     *
     * @param mallId
     * @return
     */
    public boolean canPrecharge(int mallId) {
        if (!ActivityMgr.activityInShowTime(DunHuangShiKuMgr.activityInfo)) {
            return false;
        }
        int now = DateHelper.getCurrentSecond();
        if (baseData == null) {
            return false;
        }
        if (now < (baseData.getTouchMallTime(mallId) + DunHuangShiKuMgr.DUNHUANGSHIKU_MALL_KEEP_TIME)) {
            return true;
        }
        return false;
    }


    public void changeItemNum(int activityId, int goodsId, long num) {
        if (num <= 0) {
            return;
        }
        initBaseData(activityId);
        for (DunHuangShiKuEventConfig config : DunHuangShiKuMgr.boxEventMap.values()) {
            Property property = PropertyHelper.parseStringToProperty(config.getParam());
            for (Integer id : property.getGoods().keySet()) {
                if (goodsId == id) {
                    baseData.addBoxKeyNum(config.getId(), num);
                }
            }
        }
    }

    private void initBaseData(int activityId) {
        if (baseData == null || baseData.getActivityId() != activityId) {
            baseData = new DunHuangShiKuServerUserData();
            baseData.setActivityId(DunHuangShiKuMgr.activityInfo.getActivityId());
            baseData.setUserId(player.getUserId());
            baseData.setInsertOption();
        }
    }
}
